package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.corona.Game;
import com.corona.game.util.TextureCache;
import com.corona.game.util.audio.AudioLoader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Locale;

/* compiled from: PkmnMap.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/OverworldAnimation.class */
public class OverworldAnimation extends Action {
    Vector2 position;
    String name;
    Music sound;
    Texture currText;
    Sprite currFrame;
    boolean flip;
    public Action.Layer layer = Action.Layer.map_0;
    HashMap<Integer, String> metadata = new HashMap<>();
    int frameNum = 1;
    int timer = 2;
    boolean firstStep = true;

    public OverworldAnimation(Game game, String name, Vector2 position, boolean flip, Action nextAction) {
        super(new Object[0]);
        this.flip = false;
        this.name = name.toLowerCase(Locale.ROOT).replace(' ', '_');
        this.position = position;
        this.flip = flip;
        this.nextAction = nextAction;
    }

    @Override // com.corona.game.Action
    public String getCamera() {
        return "map";
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        if (this.firstStep) {
            try {
                Reader reader = Gdx.files.internal("attacks/" + this.name + "/metadata.out").reader();
                BufferedReader br = new BufferedReader(reader);
                while (true) {
                    String line = br.readLine();
                    if (line == null) {
                        break;
                    }
                    this.metadata.put(Integer.valueOf(Integer.valueOf(line.split(", ")[0]).intValue()), line.split(", ")[1]);
                }
                reader.close();
                if (!this.name.contains("evolve")) {
                    this.sound = AudioLoader.loadMusic("attacks/" + this.name + "/sound.ogg");
                    this.sound.play();
                }
            } catch (GdxRuntimeException e) {
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
            this.firstStep = false;
            int i = 0;
            while (true) {
                FileHandle filehandle = Gdx.files.internal("attacks/" + this.name + "/output/frame-" + String.format(Locale.ROOT, "%03d", Integer.valueOf(i)) + ".png");
                if (!filehandle.exists()) {
                    break;
                }
                TextureCache.get(filehandle);
                i++;
            }
        }
        FileHandle filehandle2 = Gdx.files.internal("attacks/" + this.name + "/output/frame-" + String.format(Locale.ROOT, "%03d", Integer.valueOf(this.frameNum)) + ".png");
        if (!filehandle2.exists()) {
            game.actionStack.remove(this);
            game.insertAction(this.nextAction);
            return;
        }
        this.currText = TextureCache.get(filehandle2);
        this.currFrame = new Sprite(this.currText, 0, 0, 160, 144);
        if (this.flip) {
            this.currFrame.flip(true, false);
        }
        game.mapBatch.draw(this.currFrame, this.position.x, this.position.y);
        if (this.metadata.containsKey(Integer.valueOf(this.frameNum))) {
            this.metadata.get(Integer.valueOf(this.frameNum));
        }
        this.timer--;
        if (this.timer <= 0) {
            this.timer = 2;
            this.frameNum++;
        }
    }
}
